﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;
using Common.Logging;
using System.Collections.Specialized;

namespace QuartzForzzl
{
    public class WriteLogScheduler
    {

        static ISchedulerFactory _sf = new StdSchedulerFactory();
        static IScheduler _sched = _sf.GetScheduler();
        static WriteLogScheduler _instance = null;
        static object lockObj = new object();

        /// <summary>
        /// 线程安全的单例对象
        /// </summary>
        public static WriteLogScheduler Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObj)
                    {
                        if (_instance == null)
                        {
                            _instance = new WriteLogScheduler();
                        }
                    }
                }
                return _instance;
            }
        }

        public void Start()
        {
            ILog log = LogManager.GetLogger(typeof(WriteLogScheduler));
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            // define the job and tie it to our HelloJob class
            IJobDetail job = JobBuilder.Create<WriteLogJob>()
                .WithIdentity("job1", "group1")
                .Build();
            // Trigger the job to run on the next round minute
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .StartAt(runTime)
                .Build();
            // Tell quartz to schedule the job using our trigger
            _sched.ScheduleJob(job, trigger);
            _sched.Start();
        }
        public void Stop()
        {
            _sched.Shutdown(true);
        }

    }
}